package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dao.ProductDao;
import com.wanxi.dao.impl.ProductDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.util.JSONCopy;
import com.wanxi.vo.ProductVO;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();

    @Override
    public ResultDTO add(ProductVO productVO) {
        ProductModel model = JSONCopy.copy(productVO, ProductModel.class);
        return ResultDTO.builder()
                .count(productDao.add(model))
                .build();
    }

    @Override
    public ResultDTO update(ProductVO productVO) {
        ProductModel model = JSONCopy.copy(productVO, ProductModel.class);
        return ResultDTO.builder()
                .count(productDao.update(model))
                .build();
    }

    @Override
    public ResultDTO delete(ProductVO productVO) {
        return null;
    }

    @Override
    public ResultDTO findById(ProductVO productVO) {
        ProductModel model= JSONCopy.copy(productVO,ProductModel.class);
        return ResultDTO.builder()
                .data(productDao.findById(model))
                .build();
    }

    @Override
    public ResultDTO count(ProductVO productVO) {
        return null;
    }

    @Override
    public ResultDTO findAll(ProductVO productVO) {
/**
 * 将vo赋值给model
 */
        ProductModel model = JSONCopy.copy(productVO, ProductModel.class);
        List<ProductModel> list = productDao.findAll(model);
        int count = productDao.count(model);
        return ResultDTO.builder()
                .count(count)
                .data(list)
                .code(0)
                .build();
    }
}
